//
//  SDViewController.m
//  UI
//
//  Created by apple on 2017/7/3.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "SDViewController.h"
#import <SDAutoLayout.h>
#import <UITableView+SDAutoTableViewCellHeight.h>
#import <UITableView+FDTemplateLayoutCell.h>
#import "SDTableViewCell.h"
#import <MJExtension.h>
#import "Model.h"
#import "RequestService.h"
@interface SDViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, copy) NSString *strL;

@property (nonatomic, assign) CGSize labelRect;

@property (nonatomic, strong) NSArray *arr;

@property (nonatomic, strong) NSMutableArray *mutArr;

@property (nonatomic, strong) NSMutableArray *mutModelArr;

@property (nonatomic, strong) Model *model;

@end

@implementation SDViewController

- (NSMutableArray *)mutModelArr{

    if (!_mutModelArr) {
        
        _mutModelArr = [NSMutableArray arrayWithCapacity:0];
    }
    
    return _mutModelArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.mutArr = [NSMutableArray arrayWithCapacity:0];
    [self service];

    
    [self createTable];
//    [self labelHeight];
    
}



- (void)createTable{

    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
 
    self.tableView.fd_debugLogEnabled = YES;//开启自适应
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[SDTableViewCell class] forCellReuseIdentifier:@"haha"];//必须这样使用register注册
    
//    NSLog(@"%@",_mutModelArr);
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self.tableView fd_heightForCellWithIdentifier:@"haha" cacheByKey:indexPath configuration:^(SDTableViewCell *cell) {
       
        cell.model = self.mutModelArr[indexPath.row];
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _mutModelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    SDTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"haha" forIndexPath:indexPath];
    
    cell.model = self.mutModelArr[indexPath.row];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)service{

    //用AFNetworking请求下数据-这个URL随便找的
    [RequestService requestSuccess:^(id requestObject) {
       
        NSDictionary *dict = requestObject[@"data"];
        NSArray *arry = dict[@"list"];
        
        //字典数组转模型数组
        NSArray *brry = [Model mj_objectArrayWithKeyValuesArray:arry];//转成model
        
        //添加到数组
        [self.mutModelArr addObjectsFromArray:brry];//此时mutModelArr中装的是model
        //self.mutModelArr----cell中的cell.model = self.mutModelArr[indexPath.row];
        
        NSLog(@"===%@",requestObject);
        
        [self.tableView reloadData];

        
    } Failed:^(id error) {
        
    }];
}

- (void)labelHeight{
    
    self.label = [[UILabel alloc] init];
    NSDictionary *dic = @{NSAttachmentAttributeName : [UIFont systemFontOfSize:20]};
    self.label.text = _strL;
    
    for (int i = 0; i < _arr.count; i++) {
        
        self.labelRect = [self.arr[i] boundingRectWithSize:CGSizeMake(320, 990) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
        
        float kl = self.labelRect.height;
        NSString *lk = [NSString stringWithFormat:@"%f",kl];
        
        NSLog(@"%lf",kl);
        
        [self.mutArr addObject:lk];
        
    }
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
